<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link rel="stylesheet" type="text/css" href="../css/common.css"/>
<link rel="stylesheet" type="text/css" href="../font/iconfont.css"/>
<style>
#dongtaibox{
	padding:5px;
	background: #fff;
	box-shadow:0 0 1px #ccc;
	-webkit-box-shadow:1px 0 1px #ccc;
	padding-bottom: 0;
}
#contetns{
	display: block;
	width:100%;
	border: 0;
	height: 18rem;
	overflow: hidden;
	padding-top: 5px;
}
#contetns:focus{
	outline: none;
}
#photos{
	flex-wrap:wrap;
	-webkit-flex-wrap: wrap;
}
.photoitem{
	display: block;
	width: 23.5%;
	background: #eee;
	margin-bottom: 4px;
	overflow: hidden;
	margin-right: 4px;
}
#addimg{
	text-align: center;
	font-size:2rem;
	color: #ccc;
	line-height: 5rem;
}
.yyshuru{
	display: block;
	text-align: center;
	height:3rem;
	line-height: 3rem;
	color: orangered;
	font-size: 14px;
}
#titleinput{
	display: block;
	width: 100%;
	height: 3rem;
	background: #fff;
	border: 0;
	padding: 0;
	margin: 0;
	padding-left: 5px;
}
.titlebox{
	position: relative;
}
</style>
</head>
<body>
<div id="jcontent" class="jcontent">
	<div id="dongtaibox">
		<div class="titlebox line-b">
			<input type="text" id="titleinput" placeholder="请输入标题..."/>
		</div>
		<textarea id="contetns" placeholder="这一刻的想法..."></textarea>
		<div id="photos" class="jflex">
		</div>
	</div>
	
	<a id="yyshuru" class="yyshuru"><i class="iconfont icon-yuyin"></i>&nbsp;&nbsp;试试语音输入</a>
	
	<!--<a class="blockbtn">确认发布</a>-->
</div>
<script src="../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/common.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var screenW;
mui.plusReady(function(){
	screenW=webapp.getScreenW()/4-10;
	var _html='<a style="height:'+screenW+'px;line-height:'+screenW+'px ;" id="addimg" class="photoitem iconfont icon-i5"></a>';
	document.getElementById("photos").innerHTML=_html;
	//选择项目事件
	mui('#jcontent').on('tap', '#addimg', function(e) {
		galleryImgs();
	});
});

//帖子保存
window.addEventListener("riconClick", function(e) {
	say();
});

/**
 * 发帖
 */
function say(){
	//判断是否填写了内容或者是上传了图片
	var content=document.getElementById("contetns").value;
	var imgs=document.querySelectorAll(".pitem");
	var title=document.getElementById("titleinput").value;
	if(webapp.util.isNull(title)){
		mui.toast("请简要填写标题");
		return false;
	}
	
	if(imgs.length==0 && webapp.util.isNull(content)){
		mui.toast("请填写发布内容");
		return false;
	}
	//获取选择的图片
	var imgs=document.querySelectorAll(".upload-img");
	webapp.showWaiting();
	if(imgs.length>0){
		aferCompress(imgs,content);
	}else{
		saveTxt(content);
	}
}


mui('#jcontent').on('tap','#yyshuru', function() {
	var text=document.getElementById("contetns");
    if(plus.os.name=='Android'&&navigator.userAgent.indexOf('StreamApp')>0){
        plus.nativeUI.toast('当前环境暂不支持语音识别插件');
        return;
    }
    var options = {};
    options.engine = 'iFly';
    plus.speech.startRecognize( options, function ( s ) {
      text.innerHTML=text.value+s;
    }, function ( e ) {
    } );
});
	

/**
 * 保存文字
 */
function saveTxt(content,imagePath){
	if(!imagePath){
		imagePath=[];
	}
	webapp.ajax({
		url:"/carLife/admin/interface/post/addSave",
		data:{
			content:document.getElementById("contetns").value,
			images:imagePath.join(","),
			title:document.getElementById("titleinput").value
		},
		success:function(json){
			//触发 社区页面的下拉操作，更新数据
			var _webview=plus.webview.getWebviewById("luntan.html");
			mui.fire(_webview,"updateList");
		}
	});
}

/**
 * 上传图片
 */
var updateCofig={ 
	method:"POST",
	timeout:10,
	retry:0
};

function aferCompress(imgs,content){
	//创建上传任务
	var task = plus.uploader.createUpload(
		webapp.config.restUrl+"/carLife/common/attach/upload",
		updateCofig,
		function ( t, status) {
			uploadOver(t,status,content);
		}
	);
	
	//获取上传的 图片
	var fileNameList=new Array();
	for(var i=0;i<imgs.length;i++){
		var _path=imgs[i].getAttribute("data-imgpath");
		//这个参数必须是字母开头，不然ios下会报错，安卓下没问题
		var tempId ="f_"+ webapp.util.randomNumber(10000000);
		task.addFile(_path,{key:tempId});
		fileNameList.push(tempId);
	}
	task.addData("fileNameList",fileNameList.join(","));
	
	webapp.debug(task)
	
	task.start();
}

/**
 * 图片上传完成
 * @param {Object} t
 * @param {Object} status
 */
function uploadOver(t, status,content){
	webapp.debug(t);
	var imagePath=[];
	var result=JSON.parse(t.responseText);
	if(result.code == '200'){
		var data = result.data;
		for(var i=0;i<data.length;i++){
			imagePath.push(data[i].relativeUrl);
		}
		saveTxt(content,imagePath);
	}else{
		webapp.closeWaiting();
		plus.nativeUI.toast("图片上传失败！");
	}
}

//相册选择
function galleryImgs(){ 
    plus.gallery.pick( function(e){
    	//进行图片压缩
    	webapp.showWaiting();
      	if(document.querySelectorAll(".photoitem").length+e.files.length>10){
			mui.toast("最多只能选择9张图片！");
			webapp.closeWaiting();
			return false;
		}
    	//开始压缩图片
    	checkImgCompress(e.files,0);
    }, function ( e ) {
    },{
    	filter:"image",
    	multiple:true,
    	system:true
    });
}

/**
 * 
 * 选择的图片进行压缩
 * @param {Object} imgpath
 * 
 */
function checkImgCompress(imgArr,i){
	if(i >= imgArr.length){
		webapp.closeWaiting();
		return false;
	}
	var oldPath=imgArr[i];
	//把现有图片改名加后缀
	var newImg=webapp.util.fileRename(oldPath,"_new");
	plus.zip.compressImage({
        src:oldPath,
        dst:newImg,
        quality: 80,
        overwrite: true,
        width: '320px'
	},
	function(result) {
		i=i+1;
		checkImgCompress(imgArr,i);
		//把压缩后的图片追加到界面上进行显示
		var newNode = document.createElement("div"); 
		newNode.className="photoitem pitem";
		newNode.innerHTML ='<img style="height:'+screenW+'px;line-height:'+screenW+'px ;" data-imgpath="'+newImg+'" class="img upload-img" src="'+newImg+'"/>';
		document.getElementById("photos").insertBefore(newNode,document.getElementById("addimg")); // 这两种方法均可实现 
 	},function(e) {
		mui.toast("图片压缩失败");
	});
}

//if(document.querySelectorAll(".photoitem").length+f.length>9){
//	mui.toast("最多只能选择9张图片！");
//	return false;
//}
//for(var i in e.files){
//	var newNode = document.createElement("div"); 
//	newNode.className="photoitem pitem";
//	newNode.innerHTML ='<img data-imgpath="'+e.files[i]+'" class="img upload-img" src="'+e.files[i]+'">'; 
//	document.getElementById("photos").insertBefore(newNode,document.getElementById("addimg")); // 这两种方法均可实现 
//}
</script>
</body>
</html>
